'''
Created on Mar 22, 2011

@author: oabalbin
'''
def vcf_header():
    '''
    ##fileformat=VCFv4.0
    ##FILTER=<ID=HARD_TO_VALIDATE,Description="MQ0 >= 4 && ((MQ0 / (1.0 * DP)) > 0.1)">
    ##FILTER=<ID=InDel,Description="Overlaps a user-input mask">
    ##FILTER=<ID=LowQual,Description="Low quality">
    ##FILTER=<ID=STAND_FILTER,Description="QUAL < 30.0 || QD < 5.0 || HRun > 5 || SB > -0.10">
    ##FILTER=<ID=SnpCluster,Description="SNPs found in clusters">
    ##FORMAT=<ID=AD,Number=.,Type=Integer,Description="Allelic depths for the ref and alt alleles in the order listed">
    ##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth (only filtered reads used for calling)">
    ##FORMAT=<ID=GL,Number=3,Type=Float,Description="Log-scaled likelihoods for AA,AB,BB genotypes where A=ref and B=alt; not applicable if site is not biallelic">
    ##FORMAT=<ID=GQ,Number=1,Type=Float,Description="Genotype Quality">
    ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
    ##INFO=<ID=AB,Number=1,Type=Float,Description="Allele Balance for hets (ref/(ref+alt))">
    ##INFO=<ID=AC,Number=.,Type=Integer,Description="Allele count in genotypes, for each ALT allele, in the same order as listed">
    ##INFO=<ID=AF,Number=.,Type=Float,Description="Allele Frequency, for each ALT allele, in the same order as listed">
    ##INFO=<ID=AN,Number=1,Type=Integer,Description="Total number of alleles in called genotypes">
    ##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">
    ##INFO=<ID=DS,Number=0,Type=Flag,Description="Were any of the samples downsampled?">
    ##INFO=<ID=Dels,Number=1,Type=Float,Description="Fraction of Reads Containing Spanning Deletions">
    '''
    header=['##fileformat=VCFv4.0','#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO']
    
    return header


def bed2vcf(ifile,ofile):
    '''
    chr17   39620565        39620566        chr17|36874091|chr17_36874091_C_T|C|T|0.5278|True|FRAG=76;VF=0.5278;DP=76
    '''
    ifile,ofile = open(ifile),open(ofile,'w')
    
    ofile.write( ",".join(vcf_header()).replace(',','\n')+'\n')
    for l in ifile:
        fiedls=l.strip('\n').split('\t')
        vcf_fields=fiedls[3].split('|')
        chr, pos=fiedls[0],fiedls[1]
        id = '%s_%s&%s'%(chr,pos,vcf_fields[2])
        oline=[chr, pos, id] + vcf_fields[3:]
        ofile.write(",".join(oline).replace(',','\t')+'\n')
    
    ifile.close()
    ofile.close()
        
        
def read_annotation_file(ifile):
    '''
HG19-LOC        SAMPLE  GENE    LOC     GENOMIC VARIANT VAR FRAGS TUMOR VAR FRAC TUMOR  TUMOR TOTAL FRAGS       TUMOR A TUMOR C TUMOR G TUMOR T NORMAL A    NORMAL C NORMAL G        NORMAL T        VAR FRAGS NORMAL        VAR FRAC NORMAL NORMAL TOTAL FRAGS      DBSNP?  MUTATION        STRAND  FRAME   OLD C
ODON    NEW CODON       OLD AA  NEW AA  PROTEIN BLOSUM  UCSC IDS        SURESELECT?     SS-GENE SS-EXON VAR FRAGS TUMOR VAR READS TUMOR VAR PCR TUMOR   GEN F
RAGS TUMOR      GEN READS TUMOR GEN PCR TUMOR
chr3:120702096  aM59    STXBP5L chr3:122184786  G       A       8       0.8889  9       8       0       1       0       1       0       2       0       1   0.3333   3       rs9862407:A/G:+:val=YES non-coding-intronic,28212-from-5p-donor +       NA      NA      NA      NA      NA      NA      NA      CCDS43137.1 sureselect-no    None    0       1L      1R      agtggaAatgctc   3       8       12      1.5000  1       1       1.0000
    'chr3:120702096', 'aM59', 'STXBP5L', 'chr3:122184786', 'G', 'A', '8', '0.8889', '9', '8', '0', '1', '0', '1', '0', '2', '0', '1', '0.3333', '3', 'rs9862407:A/G:+:val=YES', 'non-coding-intronic,28212-from-5p-donor', '+', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'CCDS43137.1', 'sureselect-no', 'None', '0', '1L', '1R', 'agtggaAatgctc', '3', '8', '12', '1.5000', '1', '1', '1.0000', '']
    '''
    
    ifile=open(ifile)
    ifile.next()
    for l in ifile:
        f = l.strip('\n').split('\t')
        
        
        print f
        
    
    
    
#ifile='/exds/users/oabalbin/projects/exome/test_am59_prev/analysis/in_house/205PHABXX.3.annotated.hg19.exons.SS30M.bed'
#ofile='/exds/users/oabalbin/projects/exome/test_am59_prev/analysis/in_house/205PHABXX.3.annotated.hg19.exons.SS30M.vcf'
#bed2vcf(ifile,ofile)
ifile='/exds/users/oabalbin/projects/exome/test_am59_prev/analysis/in_house/aM59-somatic-hg19.txt'
read_annotation_file(ifile)
